package com.allwees.bs.module.cms.rest;

import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.cms.entity.ChannelEntity;
import com.allwees.bs.module.cms.req.ChannelReq;
import com.allwees.bs.module.cms.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 渠道相关 api
 */
@RestController
@RequestMapping("/backstage/api/cms/channel")
public class ChannelApi {
    @Autowired
    private ChannelService channelService;

    /**
     * 查询渠道分页列表
     *
     * @return 渠道分页
     */
    @GetMapping
    public R page() {
        List<ChannelEntity> channelEntities = channelService.list();
        return R.ok(channelEntities);
    }

    /**
     * 添加一个渠道
     *
     * @param req 请求对象
     * @return 添加状态
     */
    @PostMapping
    public R postChannel(@RequestBody @Validated ChannelReq req) {
        String uuid = channelService.getGenerateUuid(req.getPlatform(), req.getName(), req.getVersion());
        ChannelEntity channelEntity = channelService.findByUuid(uuid);
        if (channelEntity != null) {
            return R.error("渠道已存在，请检查平台、名称或版本号是否重复。");
        }

        channelEntity = new ChannelEntity();
        channelEntity.setUuid(uuid);
        channelEntity.setPlatform(req.getPlatform());
        channelEntity.setName(req.getName());
        channelEntity.setVersion(req.getVersion());
        channelEntity.setComment(req.getComment());
        channelEntity.setCreatedAt(new Date());

        boolean insertOk = channelService.saveOrUpdate(channelEntity);
        if (!insertOk) {
            return R.error("创建失败。");
        }

        return R.ok();
    }

    /**
     * 更新一个渠道
     *
     * @param req 请求对象
     * @return 添加状态
     */
    @PutMapping("{uuid}")
    public R updateChannel(@PathVariable("uuid") String uuid,
                           @RequestBody @Validated ChannelReq req) {
        ChannelEntity channelEntity = channelService.findByUuid(uuid);
        if (channelEntity == null) {
            return R.error("渠道不存在。");
        }

        String newUuid = channelService.getGenerateUuid(req.getPlatform(), req.getName(), req.getVersion());

        if (!channelEntity.getUuid().contentEquals(newUuid)) {
            ChannelEntity one = channelService.findByUuid(newUuid);
            if (one != null) {
                return R.error("需要修改的渠道已存在，请检查平台、名称或版本号是否和原有的记录重复。");
            }
        }

        channelEntity.setPlatform(req.getPlatform());
        channelEntity.setName(req.getName());
        channelEntity.setVersion(req.getVersion());
        channelEntity.setComment(req.getComment());
        channelEntity.setUpdatedAt(new Date());

        boolean insertOk = channelService.saveOrUpdate(channelEntity);
        if (!insertOk) {
            return R.error("修改失败。");
        }

        return R.ok();
    }

    /**
     * 删除一个渠道
     *
     * @param uuid 渠道 ID
     * @return 添加状态
     */
    @DeleteMapping("{uuid}")
    public R deleteChannel(@PathVariable("uuid") String uuid) {
        boolean insertOk = channelService.deleteByUuid(uuid);
        if (!insertOk) {
            return R.error("删除失败。");
        }

        return R.ok();
    }
}
